package com.ruoyi.back.service.impl

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.app.domain.vo.FunctionIndexVo
import com.ruoyi.back.domain.Function
import com.ruoyi.back.mapper.FunctionMapper
import com.ruoyi.back.service.FunctionService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.stereotype.Service

@Service
class FunctionServiceImpl : ServiceImpl<FunctionMapper, Function>(), FunctionService {

    override fun functionPage(): Page<Function> =
        TableSupport.buildPageRequest().let {
            page(
                Page(it.pageNum.toLong(), it.pageSize.toLong()),
                KtQueryWrapper(Function::class.java).orderByAsc(Function::orderNum)
            )
        }

    override fun saveFunction(function: Function): Data<Boolean> =
        function.id?.run {
            Ok("更新功能菜单成功", updateById(function))
        } ?: Ok("保存功能菜单成功", save(function))

    override fun getFaceId(): Pair<Long, Long> =
        1L to 2L

    override fun appFunctionIndex(): List<FunctionIndexVo> =
        listOf(
            FunctionIndexVo().apply {
                id = -1L
                name = "全部"
            },

            getById(1).let {
                FunctionIndexVo().apply {
                    id = it.id
                    name = it.name
                }
            },

            getById(2).let {
                FunctionIndexVo().apply {
                    id = it.id
                    name = it.name
                }
            },
        )

    fun getFunctionByName(name: String): Function? = getOne(
        KtQueryWrapper(Function::class.java)
            .eq(Function::name, name)
    )

    override fun getFunctionNameById(functionId: Long): String? =
        getById(functionId)?.name
}